package com.wewins.fota.service.export;

import com.wewins.fota.bean.bo.excel.DataSource;
import com.wewins.fota.bean.bo.excel.ExcelHeaderItem;
import com.wewins.fota.bean.bo.excel.ExportData;
import com.wewins.fota.bean.bo.excel.data.ExportFotaLog;
import com.wewins.fota.bean.po.FotaLog;
import com.wewins.fota.common.constant.CommonConst;
import com.wewins.fota.common.constant.FotaCode;
import com.wewins.fota.service.ExportService;
import com.wewins.fota.service.FotaLogService;
import lombok.Builder;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Data
@Builder
@Slf4j
public class ExportFotaLogModel implements ExportModel {
    private String path;

    private ExportData data;

    private ExportService service;

    @Override
    public <T> List<DataSource> fillData(List<T> data) {
        List<DataSource> excelData = new ArrayList<>();
        data.forEach(item -> {
            if (!(item instanceof FotaLog)) {
                return;
            }
            FotaLog fotaLog = (FotaLog) item;
            ExportFotaLog exportFotaLog = ExportFotaLog.builder().fotaLog(fotaLog).build();
            if (fotaLog.getOperator() == CommonConst.LOG_OPERATE_QUERY) {
                exportFotaLog.setStatus(!"-".equals(fotaLog.getToV()) ? FotaCode.HAS_UPDATE : FotaCode.NO_UPDATE);
            } else {  // 下载日志，如果目标版本已经作为当前版本进行过查询，则表示本次下载已成功
                if (!(service instanceof FotaLogService)) {
                    exportFotaLog.setStatus(FotaCode.NONE);
                } else if (((FotaLogService) service).downloadSuccess(fotaLog)) {
                    exportFotaLog.setStatus(FotaCode.DOWNLOAD_OK);
                } else {
                    exportFotaLog.setStatus(FotaCode.DOWNLOAD_FAILED);
                }
            }
            excelData.add(exportFotaLog);
        });
        return excelData;
    }

    @Override
    public List<ExcelHeaderItem> getHeaders() {
        return this.getHeaders(ExportFotaLog.class);
    }
}
